﻿using CIS.Common.Dtos.CommandDtos;
using CIS.Common.Dtos.ReportDtos;
using CIS.Common.Entitys;
using CIS.Common.Enums;
using CIS.Common.SignalR;
using CIS.Server.Repository.InlineToolRepository;
using CIS.Server.Repository.PortRepository;
using CIS.Server.Repository.UnitRepository;
using MediatR;

namespace CIS.Server.Manager.Equipment
{
    public class EquipmentManager : IEquipmentManager
    {
        private readonly IPortRepository _portRepository;
        private readonly IUnitRepository _unitRepository;
        private readonly IInlineToolRepository _inlineToolRepository;
        private readonly IMediator _mediator;

        public EquipmentManager(IPortRepository portRepository,IUnitRepository unitRepository,
            IInlineToolRepository inlineToolRepository, IMediator mediator)
        {
            _portRepository = portRepository;
            _unitRepository = unitRepository;
            this._inlineToolRepository = inlineToolRepository;
            _mediator = mediator;
        }


        #region Port 相关
        public async Task<PortEntity> GetPortByPortIdAsync(string portId)
        {
            PortEntity port =await _portRepository.GetFirst(p=>p.PortId == portId);
            return port;
        }

        public async Task<List<PortEntity>> GetPortsBySourceIdAsync(string portSourceId)
        {
            return await _portRepository.GetManyAsync(p=>p.QualifiedLocationId == portSourceId);
        }

        public async Task<List<PortEntity>> GetAllPortsAsync()
        {
            return await _portRepository.GetAllAsync();
        }

        public async Task<PortControlCommandDto> SendPortCommand(string sourceId, PortControlCommandDto command)
        {
            return await _mediator.Send(command);
        }

        public async Task<bool> UpdatePortAsync(PortEntity entity)
        {
            return await _portRepository.UpdateAsync(entity);
        }

        public async Task<bool> UpdatePortsManyAsync(List<PortEntity> entitys)
        {
            return await _portRepository.UpdateManyAsync(entitys);
        }

        #endregion

        #region Unit 相关
        public Task<UnitEntity> GetUnitByIdAsync(string id)
        {
            return null;
        }

        public async Task<UnitEntity> GetUnitBySourceIdAsync(string sourceId)
        {
            return await _unitRepository.GetFirst(u => u.QualifiedLocationId.Contains(sourceId));
        }

        public async Task<List<UnitEntity>> GetUnitByInLineToolIdAsync(string inlineToolId)
        {
            return await _unitRepository.GetManyAsync(u => u.InlineToolId == inlineToolId);
        }

        public async Task SendUnitStatusChangeReportAsync(UnitStatusChangeReportDto unitStatus)
        {
            Dictionary<string, object> message = new Dictionary<string, object>();            
            message["EventName"] = "UnitStatusChangeReport";
            message["Status"] = unitStatus;
            Worker.SendDataToClient(message);
        }

        public Task SendPortStatusChangeReportAsync(PortStatusChangeReportDto portStatusChangeReport)
        {
            throw new NotImplementedException();
        }
        #endregion



        #region InlineTool 相关
        public async Task<List<InlineToolEntity>>? GetInlineToolsAsync()
        {
            return await _inlineToolRepository.GetAllAsync();
        }
        #endregion
    }
}
